<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线考试平台</title>
    <link rel="shortcut icon" href="./images/exam.png" type="image/x-icon">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">

</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15" style="background: #f7f8fc">
        <div class="layui-tab layui-tab-brief" lay-filter="question_type">
            <ul class="layui-tab-title">
                <li class="layui-this" value="0">单选题</li>
                <li value="1">判断题</li>
                <li value="2">简答题</li>
                <li value="3">填空题</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-sm12 layui-col-md12">
                        <div class="layui-card">
                            <div class="layui-card-body ">
                                <table class="layui-hide" id="question_list" lay-filter="question_list"></table>
                                <script type="text/html" id="barDemo">
                                    <a class="layui-btn layui-btn-xs" lay-event="edit">保存</a>
                                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                                </script>
                                <script type="text/html" id="c_describe">
                                    <xmp>{{d.c_describe}}</xmp>
                                </script>
                                <script type="text/html" id="c_a">
                                    <xmp>{{d.c_a}}</xmp>
                                </script>
                                <script type="text/html" id="c_b">
                                    <xmp>{{d.c_b}}</xmp>
                                </script>
                                <script type="text/html" id="c_c">
                                    <xmp>{{d.c_c}}</xmp>
                                </script>
                                <script type="text/html" id="c_d">
                                    <xmp>{{d.c_d}}</xmp>
                                </script>
                                <script type="text/html" id="j_describe">
                                    <xmp>{{d.j_describe}}</xmp>
                                </script>
                                <script type="text/html" id="j_analysis">
                                    <xmp>{{d.j_analysis}}</xmp>
                                </script>
                                <script type="text/html" id="fb_describe">
                                    <xmp>{{d.fb_describe}}</xmp>
                                </script>
                                <script type="text/html" id="fb_analysis">
                                    <xmp>{{d.fb_analysis}}</xmp>
                                </script>
                                <script type="text/html" id="fb_answer">
                                    <xmp>{{d.fb_answer}}</xmp>
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/javascript"  src="../lib/layui/layui.js"></script>
<script type="text/javascript" src="../js/xadmin.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    layui.use(['element', "form", 'jquery', 'layer', 'table'], function () {
        const form = layui.form;
        const $ = layui.jquery;
        const layer = layui.layer;
        const element = layui.element;
        const table = layui.table;
        const s_id = "${s_id}";
        get_single_choiceQ()

        element.on('tab(question_type)', function (data) {
            var type = $(this).attr('value')
            if (type === '0') {
                get_single_choiceQ()

            } else if (type === '1') {
                get_judgmentQ()
            } else if (type === '2') {
                get_short_answerQ()
            }
            else if (type === '3') {
                get_fill_blanksQ()
            }
        });
    });


    function get_fill_blanksQ() {
        layui.use(["form", 'jquery', 'layer', 'table'], function () {
            const $ = layui.jquery;
            const layer = layui.layer;
            const table = layui.table;
            const s_id = "${s_id}";
            table.render({
                elem: '#question_list'
                , url: 'get_fill_blanksQ_by_s_id'
                , where: {s_id: s_id}
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , title: '试题表'
                , cols: [[
                    {field: 'fb_id', title: '试题序号', sort: true}
                    , {field: 'fb_describe', title: '试题描述', templet: '#fb_describe', edit: "text"}
                    , {field: 'fb_answer', title: '试题答案(使用","分割)', templet: '#fb_answer',edit: "text"}
                    , {field: 'fb_analysis', title: '试题解析', templet: '#fb_analysis', edit: "text"}
                    , {field: 'fb_level', title: '难度等级', edit: "text"}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: {				//分页设置
                    limit: 10,		//默认每一页显示数量
                    limits: [5, 10, 20, 30, 40, 50]		//可选择每页显示的数量
                }
                , parseData: function (res) { //res 即为原始返回的数据
                    var current_pages;
                    //第一次显示的时候this.page=true，把这种情况单独列出
                    if (this.page === true) current_pages = 1;
                    else current_pages = this.page.curr;
                    //根据分页要求选出需要显示的数据
                    let data = res.result.slice(this.limit * (current_pages - 1), this.limit * current_pages);
                    //alert(JSON.stringify(data));
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.result.length,
                        "data": data
                    }
                }
            });

            //监听行工具事件
            table.on('tool(question_list)', function (obj) {
                let data = obj.data;
                console.log(obj)
                if (obj.event === 'edit') {
                    layer.alert('确定要对试题序号' + JSON.stringify(data.fb_id) + '的修改进行保存吗？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_fill_blanksQ_by_fb_id',
                                dataType: 'json',
                                data: {
                                    fb_id:data.fb_id,
                                    fb_describe: data.fb_describe,
                                    fb_answer: data.fb_answer,
                                    fb_analysis: data.fb_analysis,
                                    fb_level: data.fb_level,
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('question_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除？', {icon: 0}, function () {
                        $.ajax({
                            url: 'del_fill_blanksQ_by_fb_id',
                            dataType: "json",//数据格式
                            type: "post",//请求方式
                            async: false,//是否异步请求
                            data: {
                                fb_id:data.fb_id,
                            },
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                        table.reload('question_list', {})
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 5, time: 1000});
                                }
                            },
                            error: function (e) {
                                alert("出现错误！！");
                            }
                        });
                    });
                }
            });

        });
    }

    function get_short_answerQ() {
        layui.use(["form", 'jquery', 'layer', 'table'], function () {
            const $ = layui.jquery;
            const layer = layui.layer;
            const table = layui.table;
            const s_id = "${s_id}";
            table.render({
                elem: '#question_list'
                , url: 'get_short_answerQ_by_s_id'
                , where: {s_id: s_id}
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , title: '试题表'
                , cols: [[
                    {field: 'sa_id', title: '试题序号', sort: true}
                    , {field: 'sa_describe', title: '试题描述', templet: '#sa_describe', edit: "text"}
                    , {field: 'sa_answer', title: '试题答案', edit: "text"}
                    , {field: 'sa_analysis', title: '试题解析', templet: '#sa_analysis', edit: "text"}
                    , {field: 'sa_level', title: '难度等级', edit: "text"}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: {				//分页设置
                    limit: 10,		//默认每一页显示数量
                    limits: [5, 10, 20, 30, 40, 50]		//可选择每页显示的数量
                }
                , parseData: function (res) { //res 即为原始返回的数据
                    var current_pages;
                    //第一次显示的时候this.page=true，把这种情况单独列出
                    if (this.page === true) current_pages = 1;
                    else current_pages = this.page.curr;
                    //根据分页要求选出需要显示的数据
                    let data = res.result.slice(this.limit * (current_pages - 1), this.limit * current_pages);
                    //alert(JSON.stringify(data));
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.result.length,
                        "data": data
                    }
                }
            });

            //监听行工具事件
            table.on('tool(question_list)', function (obj) {
                let data = obj.data;
                console.log(obj)
                if (obj.event === 'edit') {
                    layer.alert('确定要对试题序号' + JSON.stringify(data.sa_id) + '的修改进行保存吗？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_short_answerQ_by_sa_id',
                                dataType: 'json',
                                data: {
                                    sa_id:data.sa_id,
                                    sa_describe: data.sa_describe,
                                    sa_answer: data.sa_answer,
                                    sa_analysis: data.sa_analysis,
                                    sa_level: data.sa_level,
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('question_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除？', {icon: 0}, function () {
                        $.ajax({
                            url: 'del_short_answerQ_by_sa_id',
                            dataType: "json",//数据格式
                            type: "post",//请求方式
                            async: false,//是否异步请求
                            data: {
                                sa_id:data.sa_id,
                            },
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                        table.reload('question_list', {})
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 5, time: 1000});
                                }
                            },
                            error: function (e) {
                                alert("出现错误！！");
                            }
                        });
                    });
                }
            });

        });
    }


    function get_judgmentQ() {
        layui.use(["form", 'jquery', 'layer', 'table'], function () {
            const $ = layui.jquery;
            const layer = layui.layer;
            const table = layui.table;
            const s_id = "${s_id}";
            table.render({
                elem: '#question_list'
                , url: 'get_judgmentQ_by_s_id'
                , where: {s_id: s_id}
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , title: '试题表'
                , cols: [[
                    {field: 'j_id', title: '试题序号', sort: true}
                    , {field: 'j_describe', title: '试题描述', templet: '#j_describe', edit: "text"}
                    , {field: 'j_answer', title: '试题答案', edit: "text"}
                    , {field: 'j_analysis', title: '试题解析', templet: '#j_analysis', edit: "text"}
                    , {field: 'j_level', title: '难度等级', edit: "text"}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: {				//分页设置
                    limit: 10,		//默认每一页显示数量
                    limits: [5, 10, 20, 30, 40, 50]		//可选择每页显示的数量
                }
                , parseData: function (res) { //res 即为原始返回的数据
                    var current_pages;
                    //第一次显示的时候this.page=true，把这种情况单独列出
                    if (this.page === true) current_pages = 1;
                    else current_pages = this.page.curr;
                    //根据分页要求选出需要显示的数据
                    let data = res.result.slice(this.limit * (current_pages - 1), this.limit * current_pages);
                    //alert(JSON.stringify(data));
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.result.length,
                        "data": data
                    }
                }
            });

            //监听行工具事件
            table.on('tool(question_list)', function (obj) {
                let data = obj.data;
                console.log(obj)
                if (obj.event === 'edit') {
                    layer.alert('确定要对试题序号' + JSON.stringify(data.j_id) + '的修改进行保存吗？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_judgmentQ_by_j_id',
                                dataType: 'json',
                                data: {
                                    j_id:data.j_id,
                                    j_describe: data.j_describe,
                                    j_answer: data.j_answer,
                                    j_level: data.j_level,
                                    j_analysis: data.j_analysis,
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('question_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除？', {icon: 0}, function () {
                        $.ajax({
                            url: 'del_judgmentQ_by_j_id',
                            dataType: "json",//数据格式
                            type: "post",//请求方式
                            async: false,//是否异步请求
                            data: {
                                j_id:data.j_id,
                            },
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                        table.reload('question_list', {})
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 5, time: 1000});
                                }
                            },
                            error: function (e) {
                                alert("出现错误！！");
                            }
                        });
                    });
                }
            });

        });
    }

    function get_single_choiceQ() {
        layui.use(["form", 'jquery', 'layer', 'table'], function () {
            const $ = layui.jquery;
            const layer = layui.layer;
            const table = layui.table;
            const s_id = "${s_id}";
            table.render({
                elem: '#question_list'
                , url: 'get_single_choiceQ_by_s_id'
                , where: {s_id: s_id}
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , title: '试题表'
                , cols: [[
                    {field: 'c_id', title: '试题序号', sort: true}
                    , {field: 'c_describe', title: '试题描述', templet: '#c_describe', edit: "text"}
                    , {field: 'c_a', title: '选项A', templet: '#c_a', edit: "text"}
                    , {field: 'c_b', title: '选项B', templet: '#c_b', edit: "text"}
                    , {field: 'c_c', title: '选项C', templet: '#c_c', edit: "text"}
                    , {field: 'c_d', title: '选项D', templet: '#c_d', edit: "text"}
                    , {field: 'c_answer', title: '试题答案', edit: "text"}
                    , {field: 'c_analysis', title: '试题解析', templet: '#c_analysis', edit: "text"}
                    , {field: 'c_level', title: '难度等级', edit: "text"}
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: {				//分页设置
                    limit: 10,		//默认每一页显示数量
                    limits: [5, 10, 20, 30, 40, 50]		//可选择每页显示的数量
                }
                , parseData: function (res) { //res 即为原始返回的数据
                    var current_pages;
                    //第一次显示的时候this.page=true，把这种情况单独列出
                    if (this.page === true) current_pages = 1;
                    else current_pages = this.page.curr;
                    //根据分页要求选出需要显示的数据
                    let data = res.result.slice(this.limit * (current_pages - 1), this.limit * current_pages);
                    //alert(JSON.stringify(data));
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.result.length,
                        "data": data
                    }
                }
            });

            //监听行工具事件
            table.on('tool(question_list)', function (obj) {
                let data = obj.data;
                console.log(obj)
                if (obj.event === 'edit') {
                    layer.alert('确定要对试题序号' + JSON.stringify(data.c_id) + '的修改进行保存吗？', {icon: 0},
                        function () {
                            $.ajax({
                                type: 'post',
                                url: 'update_single_choiceQ_by_c_id',
                                dataType: 'json',
                                data: {
                                    c_id: data.c_id,
                                    c_describe: data.c_describe,
                                    c_a: data.c_a,
                                    c_b: data.c_b,
                                    c_c: data.c_c,
                                    c_d: data.c_d,
                                    c_answer: data.c_answer,
                                    c_level: data.c_level,
                                    c_analysis: data.c_analysis
                                },
                                success: function (data) {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                            table.reload('question_list', {})
                                        });
                                    } else {
                                        layer.msg(data.message, {icon: 5, time: 1000});
                                    }
                                }
                            });
                        }
                    );
                } else if (obj.event === 'del') {
                    layer.confirm('确认删除？', {icon: 0}, function () {
                        $.ajax({
                            url: 'del_single_choiceQ_by_c_id',
                            dataType: "json",//数据格式
                            type: "post",//请求方式
                            async: false,//是否异步请求
                            data: {
                                c_id: data.c_id
                            },
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                        table.reload('question_list', {})
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 5, time: 1000});
                                }
                            },
                            error: function (e) {
                                alert("出现错误！！");
                            }
                        });
                    });
                }
            });

        });
    }


    function getDateDiff(dateTimeStamp) {
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - dateTimeStamp;
        if (diffValue < 0) {
            //若日期不符则弹出窗口告之
            //alert("结束日期不能小于开始日期！");
        }
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        if (monthC >= 1) {
            result = "创建于" + parseInt(monthC) + "个月前";
        } else if (weekC >= 1) {
            result = "创建于" + parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            result = "创建于" + parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            result = "创建于" + parseInt(hourC) + "个小时前";
        } else if (minC >= 1) {
            result = "创建于" + parseInt(minC) + "分钟前";
        } else
            result = "刚刚创建";
        return result;
    }

    //js函数代码：字符串转换为时间戳
    function getDateTimeStamp(dateStr) {
        var time_stamp = Date.parse(dateStr.replace(/-/gi, "/"));
        return getDateDiff(time_stamp)
    }
</script>

</html>